package com.hlj.ncp.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlj.ncp.business.entity.Customer;
import com.hlj.ncp.business.service.ICustomerService;
import com.hlj.ncp.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 客户管理 控制器
 */
@Controller
@RequestMapping("/business/customer")
public class CustomerController {
    
    @Autowired
    private ICustomerService customerService;
    
    /**
     * 获取客户列表
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(Customer customer, 
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        // 验证分页参数
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        Page<Customer> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        
        // 设置查询条件
        if (customer.getCustomerName() != null && !customer.getCustomerName().isEmpty()) {
            queryWrapper.like(Customer::getCustomerName, customer.getCustomerName());
        }
        if (customer.getCustomerType() != null && !customer.getCustomerType().isEmpty()) {
            queryWrapper.eq(Customer::getCustomerType, customer.getCustomerType());
        }
        if (customer.getVipFlag() != null && !customer.getVipFlag().isEmpty()) {
            queryWrapper.eq(Customer::getVipFlag, customer.getVipFlag());
        }
        if (customer.getStatus() != null && !customer.getStatus().isEmpty()) {
            queryWrapper.eq(Customer::getStatus, customer.getStatus());
        }
        
        queryWrapper.orderByDesc(Customer::getCreateTime);
        
        Page<Customer> resultPage = customerService.page(page, queryWrapper);
        return AjaxResult.success(resultPage);
    }
    
    /**
     * 获取客户详细信息
     */
    @ResponseBody
    @GetMapping("/{customerId}")
    public AjaxResult getInfo(@PathVariable Long customerId) {
        return AjaxResult.success(customerService.getById(customerId));
    }
    
    /**
     * 新增客户
     */
    @ResponseBody
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer) {
        return customerService.save(customer) ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 修改客户
     */
    @ResponseBody
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer) {
        return customerService.updateById(customer) ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 删除客户
     */
    @ResponseBody
    @DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds) {
        for (Long customerId : customerIds) {
            customerService.removeById(customerId);
        }
        return AjaxResult.success();
    }

    /**
     * 返回客户管理页面
     */
    @GetMapping("")
    public String index() {
        return "business/customer/index";
    }
} 